package org.wanho.ums;

import org.wanho.jdbc.JdbcTemplate;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-04-21 14:37
 * Description：<描述>
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {

    private JdbcTemplate<User> jt = new JdbcTemplate<>();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   id,name,pwd,age,hobby,info,sex ")
                .append(" from ")
                .append("   t_user ")
                .toString();
        List<User> list = jt.query(sql, new UserMapper());

        resp.setContentType("text/html;charset=utf8");
        PrintWriter out = resp.getWriter();

        out.print("<!DOCTYPE html>");
        out.print("<html lang=\"en\">");
        out.print("<head>");
        out.print("    <meta charset=\"UTF-8\">");
        out.print("    <title>Title</title>");
        out.print("    <style>");
        out.print("        .container{");
        out.print("            width: 70%;");
        out.print("            margin: 0 auto;");
        out.print("            padding: 20px;");
        out.print("        }");
        out.print("        table{");
        out.print("            width: 100%;");
        out.print("            border-collapse: collapse;");
        out.print("            text-align: center;");
        out.print("        }");
        out.print("        th,td{");
        out.print("            border: 1px solid #ccc;");
        out.print("        }");
        out.print("        a{");
        out.print("            text-decoration: none;");
        out.print("            color: blue;");
        out.print("            font-weight: bold;");
        out.print("        }");
        out.print("    </style>");
        out.print("</head>");
        out.print("<body>");
        out.print("    <div class=\"container\">");
        out.print("        <a href=\"add.html\">添加用户</a>");
        out.print("        <table>");
        out.print("            <thead>");
        out.print("                <tr>");
        out.print("                    <th>编号</th>");
        out.print("                    <th>姓名</th>");
        out.print("                    <th>密码</th>");
        out.print("                    <th>年龄</th>");
        out.print("                    <th>性别</th>");
        out.print("                    <th>简介</th>");
        out.print("                    <th>爱好</th>");
        out.print("                    <th>操作</th>");
        out.print("                </tr>");
        out.print("            </thead>");
        out.print("            <tbody>");

        for (User u : list) {
            out.print("                <tr>");
            out.print("                    <td>"+u.getId()+"</td>");
            out.print("                    <td>"+u.getName()+"</td>");
            out.print("                    <td>"+u.getPwd()+"</td>");
            out.print("                    <td>"+u.getAge()+"</td>");
            out.print("                    <td>"+u.getSex()+"</td>");
            out.print("                    <td>"+u.getInfo()+"</td>");
            out.print("                    <td>"+u.getHobby()+"</td>");
            out.print("                    <td>");
            out.print("                        <a href=\"\">删除</a>");
            out.print("                        <a href=\"\">修改</a>");
            out.print("                    </td>");
            out.print("                </tr>");
        }

        out.print("            </tbody>");
        out.print("        </table>");
        out.print("    </div>");
        out.print("</body>");
        out.print("</html>");


    }
}
